package com.dream.sys.controller;

import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.dream.sys.base.BaseController;



/**
 * 
 * @description：主页
 * @author zhouy
 * @date 2018-2-14
 */

@Controller
public class UploadController  extends BaseController {

    @PostMapping("/images")
	@ResponseBody
	public Map<String,Object> updateImage(@RequestParam(value = "file", required = false) MultipartFile file,
			HttpServletRequest request) {
		Map<String,Object>  map = new HashMap<String,Object> ();
		Map<String,Object>  map1 = new HashMap<String,Object> ();
		String path = request.getSession().getServletContext().getRealPath("static");
		path += File.separator + "img";
		//System.out.println(path);
		creatFile(path);//创建文件夹
		String fileName = file.getOriginalFilename();
		String realName = UUID.randomUUID().toString()
				+ fileName.substring(fileName.lastIndexOf("."), fileName.length());
		File targetFile = new File(path, realName);

		try {
			file.transferTo(targetFile);
			map.put("code", 0);
			map.put("msg", "图片上传成功");
			//String tepmpImagePath = "img" + File.separator + realName;
			String imagePath = request.getServletContext().getContextPath() + "/static/img/" + realName;
			imagePath = imagePath.replaceAll("\\\\", "/");
			map1.put("src", imagePath);
			map.put("data", map1);
		} catch (Exception e) {
			e.printStackTrace();
			map.put("code", 1);
			map.put("msg", "图片上传失败");
			map1.put("src", path);
			map.put("data", map1);
			return map;
		}
		return map;
	}
    
    @PostMapping("/file")
	@ResponseBody
	public Map<String,Object> updateFile(@RequestParam(value = "file", required = false) MultipartFile file,
			HttpServletRequest request) {
		Map<String,Object>  map = new HashMap<String,Object> ();
		Map<String,Object>  map1 = new HashMap<String,Object> ();
		String path = request.getSession().getServletContext().getRealPath("static");
		path += File.separator + "img";
		//System.out.println(path);
		creatFile(path);//创建文件夹
		String fileName = file.getOriginalFilename();
		String realName = UUID.randomUUID().toString()
				+ fileName.substring(fileName.lastIndexOf("."), fileName.length());
		File targetFile = new File(path, realName);

		try {
			file.transferTo(targetFile);
			map.put("code", 0);
			map.put("msg", "文件上传成功");
			//String tepmpImagePath = "img" + File.separator + realName;
			String imagePath = request.getServletContext().getContextPath() + "/resource/img/" + realName;
			imagePath = imagePath.replaceAll("\\\\", "/");
			map1.put("src", imagePath);
			map.put("data", map1);
		} catch (Exception e) {
			e.printStackTrace();
			map.put("code", 1);
			map.put("msg", "文件上传失败");
			map1.put("src", path);
			map.put("data", map1);
			return map;
		}
		return map;
	}
}
